package cc.leap.service;

import cc.leap.mapper.SysLogininforMapper;
import cc.leap.model.PageQuery;
import cc.leap.model.TableDataInfo;
import cc.leap.model.domain.SysLogininfor;
import com.mybatisflex.core.paginate.Page;
import com.mybatisflex.core.query.QueryWrapper;
import com.mybatisflex.solon.service.impl.ServiceImpl;
import org.noear.solon.annotation.Component;
import org.noear.solon.annotation.Inject;

import static cc.leap.model.domain.table.SysLogininforTableDef.SYS_LOGININFOR;

/**
 * SysLogininforService
 *
 * @author leap
 * @date 2024/8/20
 */
@Component
public class SysLogininforService extends ServiceImpl<SysLogininforMapper, SysLogininfor> {

    @Inject
    SysLogininforMapper logininforMapper;

    /**
     * 根据logininforBo构建QueryWrapper查询条件
     *
     * @param logininforBo
     * @return 查询条件
     */
    private QueryWrapper buildQueryWrapper(SysLogininfor logininforBo) {
        QueryWrapper queryWrapper = QueryWrapper.create()
                .select(SYS_LOGININFOR.INFO_ID, SYS_LOGININFOR.USER_NAME, SYS_LOGININFOR.IPADDR, SYS_LOGININFOR.LOGIN_LOCATION, SYS_LOGININFOR.BROWSER, SYS_LOGININFOR.OS, SYS_LOGININFOR.STATUS, SYS_LOGININFOR.MSG, SYS_LOGININFOR.LOGIN_TIME)
                .and(SYS_LOGININFOR.IPADDR.like(logininforBo.getIpaddr()))
                .and(SYS_LOGININFOR.STATUS.eq(logininforBo.getStatus()))
                .and(SYS_LOGININFOR.USER_NAME.like(logininforBo.getUserName()))
                .and(SYS_LOGININFOR.LOGIN_TIME.between(logininforBo.getParams().get("beginTime"), logininforBo.getParams().get("endTime")))
                .orderBy(SYS_LOGININFOR.INFO_ID.desc());
        return queryWrapper;
    }
    /**
     * 新增系统登录日志
     *
     * @param logininfor 访问日志对象
     */
    public void insertLogininfor(SysLogininfor logininfor) {
        logininforMapper.insertLogininfor(logininfor);
    }

    /**
     * 查询系统登录日志集合
     *
     * @param logininfor 访问日志对象
     * @return 登录记录集合
     */
    public TableDataInfo<SysLogininfor> selectLogininforPage(SysLogininfor logininfor) {
        QueryWrapper queryWrapper = buildQueryWrapper(logininfor);
        Page<SysLogininfor> page = this.pageAs(PageQuery.build(), queryWrapper, SysLogininfor.class);
        return TableDataInfo.build(page);
    }
}
